package com.study.partition;

import com.alibaba.fastjson.JSON;
import com.study.vo.StudyMessage;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.stream.binder.PartitionKeyExtractorStrategy;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

/**
 * 自定义从 message 中提取 partition key 的策略
 */
@Slf4j
@Component
public class JackPartitionKeyExtractorStrategy implements PartitionKeyExtractorStrategy{
    @Override
    public Object extractKey(Message<?> message) {

        StudyMessage messageObj = JSON.parseObject(
                message.getPayload().toString(),
                StudyMessage.class
        );

        //自定义提取key
        String key = messageObj.getProjectName();

        log.info("springcloud stream jack partition key : [{}]",
                key);
        return key;
    }
}
